package main

import (
	"fmt"
	"strings"
)

//双指针:8ms 28%,7MB 14%
func reverseWords(s string) string {
	i, j := 0, 0
	res := ""
	for j < len(s) {
		for i = j; i < len(s); i++ {
			if s[i] != ' ' {
				break
			}
		}
		for j = i + 1; j < len(s); j++ {
			if s[j] == ' ' {
				break
			}
		}
		if i < len(s) {
			res = s[i:j] + " " + res
		}
	}
	return res
}

//strings库函数:8ms 28%,7.1MB 5%
func reverseWords1(s string) string {
	s = strings.Trim(s, " ")
	words := strings.Fields(s)
	result := ""
	for i := len(words) - 1; i >= 0; i-- {
		result += words[i]
		if i != 0 {
			result += " "
		}
	}
	return result
}

func main() {
	s := "       hello         world"
	count := 0
	for range s {
		count++
	}
	fmt.Println(count, len(s))
	fmt.Printf("%T\n", s[1:4])
	fmt.Println(reverseWords(s))
	fmt.Println(reverseWords1(s))
	s = "hello" + s
	fmt.Println(s)
}
